﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Infrastructure;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc;
using SnowTech.SmartTms.App;
using SnowTech.SmartTms.App.BenzinkostenManager;
using SnowTech.SmartTms.App.Interface;
using SnowTech.SmartTms.App.Request;
using SnowTech.SmartTms.App.TmsOilCostManager.Request;
using SnowTech.SmartTms.Repository.Domain;

namespace SnowTech.SmartTms.Mvc.Controllers
{
    public class TmsOilCostMangerController : BaseController
    {
        private readonly TmsOilCostApp _app;
        private readonly RevelanceManagerApp _revelanceManagerApp;
        public TmsOilCostMangerController(IAuth authUtil, RevelanceManagerApp revelanceManagerApp, TmsOilCostApp app) : base(authUtil)
        {
            _revelanceManagerApp = revelanceManagerApp;
            _app = app;
        }
        /// <summary>
        /// 显示视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }    
        //添加油费
        [HttpPost]
        public string Add(AddOrUpdateOilCostReq obj)//req结尾的dto表示输入参数，必须对其进行验证
        {
            Response resp = new Response();   //定义一个返回模型对象
            try
            {
                _app.Add(obj);   //调用Appservice的添加方法

            }
            catch (Exception ex)
            {
                resp.Code = 500;
                resp.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return JsonHelper.Instance.Serialize(resp);
        }

        //修改油费
        [HttpPost]
        public string Update(AddOrUpdateOilCostReq obj)
        {
            try
            {
                _app.Update(obj);

            }
            catch (Exception ex)
            {
                Result.Code = 500;
                Result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return JsonHelper.Instance.Serialize(Result);
        }

        /// <summary>
        /// 加载列表
        /// </summary>
        [HttpGet]
        public async Task<string> Show([FromQuery] QueryTmsOilCostReq request)
        {
            var objs = await _app.Load(request);
            return JsonHelper.Instance.Serialize(objs);
        }

        [HttpPost]
        public string Delete(string[] ids)
        {
            try
            {
                _app.Delete(ids);
            }
            catch (Exception e)
            {
                Result.Code = 500;
                Result.Message = e.InnerException?.Message ?? e.Message;
            }

            return JsonHelper.Instance.Serialize(Result);
        }
    }
}
